// Copyright 2007-2014 Metaio GmbH. All rights reserved.
package com.metaio.mycube3d;

import java.io.File;

import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.metaio.Template.R;
import com.metaio.sdk.ARViewActivity;
import com.metaio.sdk.MetaioDebug;
import com.metaio.sdk.jni.ETRACKING_STATE;
import com.metaio.sdk.jni.EVISUAL_SEARCH_STATE;
import com.metaio.sdk.jni.IGeometry;
import com.metaio.sdk.jni.IMetaioSDKCallback;
import com.metaio.sdk.jni.IVisualSearchCallback;
import com.metaio.sdk.jni.ImageStruct;
import com.metaio.sdk.jni.TrackingValues;
import com.metaio.sdk.jni.TrackingValuesVector;
import com.metaio.sdk.jni.VisualSearchResponseVector;
import com.metaio.tools.io.AssetsManager;

public class Template extends ARViewActivity {
	
	private IGeometry mAidCube;
	private IGeometry mCube;
	private MetaioSDKCallbackHandler mSDKCallback;
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		
		mSDKCallback = new MetaioSDKCallbackHandler();

	}
	

	@Override
	protected void onDestroy(){
		super.onDestroy();
		mSDKCallback.delete();
		mSDKCallback = null;
	}
	
	@Override
	protected int getGUILayout() 
	{
		// Attaching layout to the activity
		return R.layout.template; 
	}
	
	@Override
	protected void loadContents() 
	{
		try
		{
			
			// Getting a file path for tracking configuration XML file
			final File trackingConfigFile = AssetsManager.getAssetPathAsFile(getApplicationContext(), "Tracking.xml");
			
			final File modelAidPath = AssetsManager.getAssetPathAsFile(getApplicationContext(), "VIS_INIT.obj");
			final File modelPath = AssetsManager.getAssetPathAsFile(getApplicationContext(), "VIS_TRACK.obj");
			
			if (modelAidPath != null) {
				mAidCube = metaioSDK.createGeometry(modelAidPath);
				mAidCube.setCoordinateSystemID(2);
			}
			
			if (modelPath!=null){
				mCube = metaioSDK.createGeometry(modelPath);
				mCube.setCoordinateSystemID(1);
			}
			
			// Assigning tracking configuration
			boolean result = metaioSDK.setTrackingConfiguration(trackingConfigFile); 
			MetaioDebug.log("Tracking data loaded: " + result); 
			
		}       
		catch (Exception e)
		{
			MetaioDebug.log(Log.ERROR, "Failed to load content: " + e);
		}
	}
	
  
	@Override
	protected void onGeometryTouched(IGeometry geometry) 
	{
		MetaioDebug.log("Template.onGeometryTouched: " + geometry);

	}


	@Override
	protected IMetaioSDKCallback getMetaioSDKCallbackHandler() 
	{
		return mSDKCallback;
	}
	
	final class MetaioSDKCallbackHandler extends IMetaioSDKCallback 
	{

		@Override
		public void onSDKReady() 
		{
			MetaioDebug.log("The SDK is ready");
			metaioSDK.sensorCommand("translateInitialPoseWorld", 10+" "+0+" ");
		}
		
		@Override
		public void onAnimationEnd(IGeometry geometry, String animationName) 
		{
			MetaioDebug.log("animation ended" + animationName);
		}
		
		@Override
		public void onMovieEnd(IGeometry geometry, File filePath)
		{
			MetaioDebug.log("movie ended" + filePath.getPath());
		}
		
		@Override
		public void onNewCameraFrame(ImageStruct cameraFrame)
		{
			MetaioDebug.log("a new camera frame image is delivered" + cameraFrame.getTimestamp());
		}
		
		@Override 
		public void onCameraImageSaved(File filePath)
		{
			MetaioDebug.log("a new camera frame image is saved to" + filePath.getPath());
		}
		
		@Override
		public void onScreenshotImage(ImageStruct image)
		{
			MetaioDebug.log("screenshot image is received" + image.getTimestamp());
		}
		
		@Override
		public void onScreenshotSaved(File filePath)
		{
			MetaioDebug.log("screenshot image is saved to" + filePath.getPath());
		}
		
		@Override
		public void onTrackingEvent(TrackingValuesVector trackingValues) {
			for (int i = 0; i < trackingValues.size(); i++) {
				final TrackingValues v = trackingValues.get(i);
				MetaioDebug.log("Tracking state for COS "
						+ v.getCoordinateSystemID()
						+ " is "
						+ v.getState()
						+ " quality: "
						+ trackingValues.get(i).getQuality()
						+ " rotation: "
						+ trackingValues.get(i).getRotation()
						+ " traslation: "
						+ trackingValues.get(i).getInverseTranslation()
								.toString());
				if(v.getCoordinateSystemID()==1&& v.isTrackingState()){
					if(!mAidCube.getTranslation().equals(v.getTranslation())){
						v.setState(ETRACKING_STATE.ETS_NOT_TRACKING);
					}
				}
				if(v.getCoordinateSystemID()==1&& !v.isTrackingState() ){
					runOnUiThread(new Runnable() {
						
						@Override
						public void run() {
							// TODO Auto-generated method stub
							Toast.makeText(getApplicationContext(), "lost", Toast.LENGTH_SHORT).show();
						}
					});
				}else if(v.getCoordinateSystemID()==1 && v.isTrackingState()){
					runOnUiThread(new Runnable() {
						public void run() {
							Toast.makeText(getApplicationContext(), "found", Toast.LENGTH_SHORT).show();
						}
					});
				}
				
			}
		}
		

		@Override
		public void onInstantTrackingEvent(boolean success, File filePath)
		{
			if (success)
			{
				MetaioDebug.log("Instant 3D tracking is successful");
			}
		}
	}
	
	final class VisualSearchCallbackHandler extends IVisualSearchCallback
	{
		@Override
		public void onVisualSearchResult(VisualSearchResponseVector response, int errorCode)
		{
			if (errorCode == 0)
			{
				MetaioDebug.log("Visual search is successful");
			}
		}

		@Override
		public void onVisualSearchStatusChanged(EVISUAL_SEARCH_STATE state) 
		{
			MetaioDebug.log("The current visual search state is: " + state);
		}
	}
}
